package com.example.yuejianapp14.Activity.Adapter;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.yuejianapp14.Activity.Activity.ActActivity;
import com.example.yuejianapp14.Activity.Activity.ClassActivity;
import com.example.yuejianapp14.Activity.JavaBean.Enter;
import com.example.yuejianapp14.R;
import com.squareup.picasso.Picasso;

import java.util.List;

import static android.content.Context.MODE_PRIVATE;

/**
 * Created by jason_lin on 2017/9/25.
 */

public class UserEnteredAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context mContext;
    private List<Enter> mDataList;
    private LayoutInflater mLayoutInflater;
    private SharedPreferences sp;

    public UserEnteredAdapter(Context mContext, List<Enter> mDataList) {
        this.mContext = mContext;
        this.mDataList = mDataList;
        mLayoutInflater = LayoutInflater.from(mContext);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = mLayoutInflater.from(parent.getContext()).inflate(R.layout.enteredcard, parent, false);
        return new UserEnteredAdapter.ViewHolder(v);
    }

    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final Enter entity = (Enter) mDataList.get(position);
        if (null == entity)
            return;
        ViewHolder viewHolder = (ViewHolder) holder;
        if (entity.getClassid() != null) {
            viewHolder.entered_name.setText(entity.getClassname());
            Picasso.with(mContext).load("http://10.0.2.2/YueJian/Public/photo/class/" + entity.getClasspic()).into(viewHolder.entered_pic);
            if(entity.getEfinish()!=null){
                viewHolder.ifEntered.setImageResource(R.drawable.entered);
            }
        }
        if (entity.getActivityid() != null) {
            viewHolder.entered_name.setText(entity.getActivity());
            Picasso.with(mContext).load("http://10.0.2.2/YueJian/Public/photo/activity/" + entity.getActivitypic()).into(viewHolder.entered_pic);
            if(entity.getEfinish()!=null){
                viewHolder.ifEntered.setImageResource(R.drawable.entered);
            }
        }

        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(entity.getEtype().equals("1")) {

                    Intent intent = new Intent(mContext, ClassActivity.class);
                    intent.putExtra("classid", entity.getClassid());
                    intent.putExtra("img",entity.getClasspic());
                    mContext.startActivity(intent);
                }
                if(entity.getEtype().equals("2")){
                    Intent intent = new Intent(mContext, ActActivity.class);
                    intent.putExtra("actid", entity.getActivityid());
                    intent.putExtra("actpic",entity.getActivitypic());
                    mContext.startActivity(intent);

                }
            }
        });
    }
        @Override
        public int getItemCount() {
            return mDataList.size();
        }
        public class ViewHolder extends RecyclerView.ViewHolder{
            ImageView entered_pic,ifEntered;
            TextView entered_name;
            LinearLayout entered_container;

            public ViewHolder(View itemView) {
                super(itemView);
                entered_name = (TextView) itemView.findViewById(R.id.entered_name);
                entered_pic = (ImageView) itemView.findViewById(R.id.entered_pic);
                entered_container = (LinearLayout) itemView.findViewById(R.id.entered_container);
                ifEntered =(ImageView) itemView.findViewById(R.id.ifEntered);
                sp = mContext.getSharedPreferences("uid", MODE_PRIVATE);
            }
        }
    }

